package com.enfi.api.modules.bz.controller;

import com.enfi.api.modules.bz.domain.DateQuery;
import com.enfi.api.util.SecUser;
import com.enfi.api.util.UserUtils;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author wind
 * @date 2020/3/12
 */
@Controller
@RequestMapping("count")
public class WarningStatisticsController {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private HttpServletRequest request;

    /**
     * select level,count(1) as count from t_tbl_warning
     * where EXISTS (select 1 from t_tbl_warning_config_role t1 where t1.role_id in ("XGZS_SBBJL") )
     * group by level
     * <p>
     * select * from js_sys_user_role limit 1
     * select role_code from js_sys_user_role where  user_code="lvpengfei_9mka"
     * <p>
     * <p>
     * select process_name,index_name,count(1) as count from t_tbl_warning where process_name is not null and level=1 and EXISTS (select 1 from t_tbl_warning_config_role t1 where t1.role_id in ("XGZS_SBBJL") ) group by process_name,index_name
     *
     * @param dateQuery
     * @return
     */
    @GetMapping("warning/level")
    @ResponseBody
    public Object getById(DateQuery dateQuery, String factory) {
        SecUser secUser= UserUtils.getSecUser();
        // List list=jdbcTemplate.queryForList("select level,count(1) as count from t_tbl_warning where warning_time>=? and warning_time<=? group by level",queryVo.getStartTime(),queryVo.getEndTime());
        NamedParameterJdbcTemplate givenParamJdbcTemp = new NamedParameterJdbcTemplate(jdbcTemplate);
        Map<String, Object> args = Maps.newHashMap();
        args.put("roleIds", secUser.getAuthoritiesToList());
        args.put("startTime", dateQuery.getStartTime());
        args.put("endTime", dateQuery.getEndTime());
        args.put("factory", factory);
        String sql = "select level,count(1) as count from t_tbl_warning where 1=1 ";
        if (dateQuery.getStartTime() != null) {
            sql += "  and warning_time>=:startTime ";
        }
        if (dateQuery.getEndTime() != null) {
            sql += " and warning_time<=:endTime ";
        }
        if (factory != null) {
            sql += "  and factory_code=:factory ";
        }
        if (secUser.getAuthoritiesToList()!= null) {
            sql += " and EXISTS (select 1 from t_tbl_warning_role t1 where t1.role_id in (:roleIds)) ";
        }
        sql += " group by level ";
        return givenParamJdbcTemp.query(sql, args,
                (rs, index) -> {
                    Map a = Maps.newHashMap();
                    a.put("level", rs.getInt("level"));
                    a.put("count", rs.getInt("count"));
                    return a;
                });
    }

    @GetMapping("warning/index")
    @ResponseBody
    public Object getById(DateQuery dateQuery, Integer level, String factory) {
        SecUser secUser= UserUtils.getSecUser();
        NamedParameterJdbcTemplate givenParamJdbcTemp = new NamedParameterJdbcTemplate(jdbcTemplate);
        Map<String, Object> args = Maps.newHashMap();
        args.put("roleIds", secUser.getAuthoritiesToList());
        args.put("startTime", dateQuery.getStartTime());
        args.put("endTime", dateQuery.getEndTime());
        args.put("factory", factory);
        args.put("level", level);
        String sql = "select process_name,index_name,count(1) as count from t_tbl_warning where process_name is not null ";
        if (dateQuery.getStartTime() != null) {
            sql += "  and warning_time>=:startTime ";
        }
        if (dateQuery.getEndTime() != null) {
            sql += " and warning_time<=:endTime ";
        }
        if (level != null) {
            sql += " and level=:level ";
        }
        if (factory != null) {
            sql += "  and factory_code=:factory ";
        }
        if (secUser.getAuthoritiesToList()!=null) {
            sql += " and EXISTS (select 1 from t_tbl_warning_role t1 where t1.role_id in (:roleIds) ) ";
        }
        sql += " group by process_name,index_name";
        return givenParamJdbcTemp.query(sql, args,
                (rs, index) -> {
                    Map a = Maps.newHashMap();
                    a.put("process_name", rs.getString("process_name"));
                    a.put("index_name", rs.getString("index_name"));
                    a.put("count", rs.getInt("count"));
                    return a;
                });
    }
}
